<?php

	/**
	 * @see Bike_Model_Mapper_Abstract
	 */
	require_once 'Bike/Model/Mapper/Abstract.php';
	
	/**
	 * @see Bike_Auth_Adapter_Mapper_Interface
	 */
	require_once 'Bike/Auth/Adapter/Mapper/Interface.php';
	
	/**
	 * @see Bike_Model_Mapper_Dependence
	 */
	require_once 'Bike/Model/Mapper/Dependence.php';
	
	
	class Bike_Model_Mapper_User extends Bike_Model_Mapper_Abstract implements Bike_Auth_Adapter_Mapper_Interface
	{
		const DB_TABLE_NAME = 'user';
		const DB_TABLE_PK_NAME = 'user_id';
		const DB_TABLE_ALIAS = 'u';

		static public function getInstance()
	    {
	        return self::_getInstance(__CLASS__);
	    }

	    public function getDefaultValues()
	    {	    	return array(
	    		'status_id' => $this->getStatusIdActive(),
	    		'date_registration' => time(),
	    		'date_update' => time(),
	    	);
	    }

	    protected function _getDependences()
	    {
	    	return array(
	    		$this->createDependence(array(
					'type' => Bike_Model_Mapper_Dependence::JOIN_INNER,
					'name' => 'auth',
					'mapperClass' => 'Bike_Model_Mapper_UserAuth',
					'parentJoinFields' => array('user_id'),
				)),
				$this->createDependence(array(
					'type' => Bike_Model_Mapper_Dependence::JOIN_LEFT,
					'name' => 'status',
					'mapperClass' => 'Bike_Model_Mapper_Status',
					'parentJoinFields' => array('status_id'),
				)),
	    	);
	    }
	    
	    public function getWindowTitleEdit(Bike_Model_Abstract $model)
	    {
	    	return $model->getAuthLogin();
	    }
	    
		public function getUserIdFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('User ID'),
		    		'order' => 10,
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getDateRegistrationFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Registration date'),
		    		'order' => 20,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'type' => 'date',
				    'disabled' => true,
	    		);
	    		case self::PARAMS_LIST_COLUMN: return array(
	    			'xtype' => 'datecolumn',
					'format' => 'd.m.Y',
	    		);
	    		case self::PARAMS_LIST_MODEL: return array(
		    		'type' => 'date',
		    		'dateFormat' => 'timestamp',
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getDateUpdateFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Update date'),
		    		'order' => 30,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'type' => 'date',
		    		'disabled' => true,
	    		);
	    		case self::PARAMS_LIST_COLUMN: return array(
		    		'xtype' => 'datecolumn',
		    		'format' => 'd.m.Y',
	    		);
	    		case self::PARAMS_LIST_MODEL: return array(
		    		'type' => 'date',
		    		'dateFormat' => 'timestamp',
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getStatusIdFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Status'),
		    		'order' => 40,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'type' => 'radio',
		    		'required' => false,
		    		'multiOptions' => Bike_Model_Mapper_Status::getInstance()->fetchAll()->getMultiOptions('status_id', 'title'),
	    		);
	    		case self::PARAMS_LIST_COLUMN: return array(
	    			'dataIndex' => $this->getLocalizedFieldName('status_title'),
	    		);
	    		default: return array();
	    	}
	    }
	    
	    /*public function getAuthUserIdFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Test'),
		    		'order' => 50,
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getAuthPasswordDFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('pass d'),
		    		'order' => 60,
	    		);
	    		default: return array();
	    	}
	    }*/
	    
	    public function getDateUpdateBeforeUpdateHandler(Bike_Model_Abstract $model)
	    {
	    	$model->setDateUpdate(Bike_Date::now()->getTimestamp());
	    }
	    
	    public function getDateUpdateBeforeInsertHandler(Bike_Model_Abstract $model)
	    {
	    	$model->setDateUpdate(Bike_Date::now()->getTimestamp());
	    }
	    
	    public function getDateRegistrationBeforeInsertHandler(Bike_Model_Abstract $model)
	    {
	    	$model->setDateRegistration(Bike_Date::now()->getTimestamp());
	    }

	    /**
	     * getIdentityColumn() - defined by Bike_Auth_Adapter_Mapper_Interface. Get the column name to be used as the identity column
	     *
	     * @return string $identityColumn
	     */
	    public function getIdentityColumn()
	    {	    	return 'auth_login';
	    }

	    /**
	     * getCredentialColumn() - defined by Bike_Auth_Adapter_Mapper_Interface. Get the column name to be used as the credential column
	     *
	     * @return  string $credentialColumn
	     */
	    public function getCredentialColumn()
	    {	    	return 'auth_password';
	    }

	    /**
	     * getCredentialTreatment() - defined by Bike_Auth_Adapter_Mapper_Interface. Get a parameterized string that is
	     * used to transform or treat the input credential data.
	     *
	     * In many cases, passwords and other sensitive data are encrypted, hashed, encoded,
	     * obscured, or otherwise treated through some function or algorithm. By specifying a
	     * parameterized treatment string with this method, a developer may apply arbitrary SQL
	     * upon input credential data.
	     *
	     * Examples:
	     *
	     *  'PASSWORD(?)'
	     *  'MD5(?)'
	     *
	     * @return  string $treatment
	     */
	    public function getCredentialTreatment()
	    {	    	return 'MD5(?)'	;
	    }

	    /**
	     * getAuthenticationSelect() - defined by Bike_Auth_Adapter_Mapper_Interface. Get the Bike_Db_Mapper_Select for authentication
	     *
	     * @return  Bike_Db_Mapper_Select
	     */
	    public function getAuthenticationSelect()
	    {	    	return $this->selectFull(array(
								        	'status' => array(
								        		'deepening' => False,
								        	),
								        	'auth' => array(
								        		'deepening' => False,
								        	),
			));
	    }
	}